package com.lzx.user.controller.web;

import com.lzx.common.utils.R;
import com.lzx.user.entity.User;
import com.lzx.user.entity.vo.OssVO;
import com.lzx.user.entity.vo.UserinfoVO;
import com.lzx.user.service.UserService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/user/user/web")
public class WebUserController {
    @Resource
    private UserService userService;

    //获取用户浏览记录
    @GetMapping("/getUserBrowseList/{userId}")
    public R getUserBrowseList(@PathVariable Long userId) {
        return userService.getUserBrowseList(userId);
    }

    //获取所有用户id
    @GetMapping("/getUserIdList")
    public List<Long> getUserIdList() {
        return userService.getUserIdList();
    }

    //获取回答数前十的用户(不包括当前登录用户)
    @GetMapping("/getUserByReplyCount/{userId}")
    public List<User> getUserByReplyCount(@PathVariable int userId) {
        return userService.getUserByReplyCount(userId);
    }

    //用户关注其他人，关注数+1，被关注的人粉丝数+1；取消关注其他人，关注数-1，被关注的人粉丝数-1
    @PutMapping("/attentionAndCancelAttention/{userId}/{isAttention}/{attentionUserId}")
    public R attentionAndCancelAttention(@PathVariable Long userId, @PathVariable Integer isAttention, @PathVariable Long attentionUserId) {
        return userService.attentionAndCancelAttention(userId, isAttention, attentionUserId);
    }

    //用户修改信息
    @PutMapping("/updateUserinfo")
    public R updateUserinfo(@RequestBody @Valid UserinfoVO userinfoVO, BindingResult result) {
        if (result.hasErrors()) {
            return R.error().message(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        return userService.updateUserinfo(userinfoVO);
    }

    //获取所有用户
    @GetMapping("/getList")
    public R getList() {
        return userService.getList();
    }

    //根据用户回答数查询前十个用户
    @GetMapping("/rankBeforeTen")
    public R rankBeforeTen() {
        return userService.rankBeforeTen();
    }

    //用户回答问题，回答数+1
    @PutMapping("/userReplyAdd/{userId}")
    public R userReplyAdd(@PathVariable Long userId) {
        return userService.userReplyAdd(userId);
    }

    //根据id获取用户信息
    @GetMapping("/{id}")
    public R getUserinfoById(@PathVariable Long id) {
        return userService.getUserinfoById(id);
    }

    //根据id获取用户信息(提供给其他服务)
    @GetMapping("/toOtherServe/{id}")
    public User getUserinfoByIdToOtherServe(@PathVariable Long id) {
        return userService.getById(id);
    }

    //用户点赞问题，用户积分+1
    @PostMapping("/likeProblem/{userId}")
    public R likeProblem(@PathVariable Integer userId) {
        return userService.likeProblem(userId);
    }

    //登录
    @PostMapping("/login")
    public R login(@RequestBody User user, HttpServletRequest request) {
        return userService.login(user, request);
    }

    //判断手机号是否已经注册
    @GetMapping("/mobileIsRegister/{mobile}")
    public R mobileIsRegister(@PathVariable String mobile) {
        return userService.mobileIsRegister(mobile);
    }

    //注册
    @PostMapping("/register")
    public R register(@RequestBody @Valid User user, BindingResult result) {
        if (result.hasErrors()) {
            return R.error().message(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        return userService.register(user);
    }

    //更新用户头像
    @PostMapping("/updateAvatar")
    public R updateAvatar(@RequestBody OssVO ossVO) {
        return userService.updateAvatar(ossVO);
    }

    //根据token获取个人信息
    @GetMapping("/getUserInfo")
    public R getUserInfo(HttpServletRequest request) {
        return userService.getUserInfo(request);
    }
}
